{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Coggle 30 Days of ML in Jan：文本相似度匹配\n",
    "\n",
    "## 项目说明\n",
    "本月竞赛学习将以文本匹配问题展开，文本匹配拥有广泛的应用场景，可以用于去除重复问题和文本相似度中。在本次学习中我们将学习：\n",
    "\n",
    "- 如何计算文本之间的统计距离\n",
    "- 如何训练词向量 & 无监督句子编码\n",
    "- BERT模型搭建和训练\n",
    "\n",
    "上述步骤都是一个NLP算法工程师必备的基础，在本月我们将逐步从基础出发，逐步解决文本匹配问题。\n",
    "\n",
    "### 背景介绍\n",
    "\n",
    "文本语义匹配是自然语言处理中一个重要的基础问题，NLP 领域的很多任务都可以抽象为文本匹配任务。例如，信息检索可以归结为查询项和文档的匹配，问答系统可以归结为问题和候选答案的匹配，对话系统可以归结为对话和回复的匹配。语义匹配在搜索优化、推荐系统、快速检索排序、智能客服上都有广泛的应用。如何提升文本匹配的准确度，是自然语言处理领域的一个重要挑战。\n",
    "\n",
    "- 信息检索：在信息检索领域的很多应用中，都需要根据原文本来检索与其相似的其他文本，使用场景非常普遍。\n",
    "- 新闻推荐：通过用户刚刚浏览过的新闻标题，自动检索出其他的相似新闻，个性化地为用户做推荐，从而增强用户粘性，提升产品体验。\n",
    "- 智能客服：用户输入一个问题后，自动为用户检索出相似的问题和答案，节约人工客服的成本，提高效率。\n",
    "\n",
    "让我们来看一个简单的例子，比较各候选句子哪句和原句语义更相近：\n",
    "\n",
    "    原句：“车头如何放置车牌”\n",
    "    比较句1：“前牌照怎么装”\n",
    "    比较句2：“如何办理北京车牌”\n",
    "    比较句3：“后牌照怎么装”\n",
    "\n",
    "比较结果：\n",
    "\n",
    "    比较句1与原句，虽然句式和语序等存在较大差异，但是所表述的含义几乎相同\n",
    "    比较句2与原句，虽然存在“如何” 、“车牌”等共现词，但是所表述的含义完全不同\n",
    "    比较句3与原句，二者讨论的都是如何放置车牌的问题，只不过一个是前牌照，另一个是后牌照。二者间存在一定的语义相关性\n",
    "    所以语义相关性，句1大于句3，句3大于句2，这就是语义匹配。\n",
    "\n",
    "### 数据说明\n",
    "\n",
    "[LCQMC](http://icrc.hitsz.edu.cn/Article/show/171.html)数据集比释义语料库更通用，因为它侧重于意图匹配而不是释义。LCQMC数据集包含 260,068 个带有人工标注的问题对。\n",
    "\n",
    "    包含 238,766 个问题对的训练集\n",
    "    包含 8,802 个问题对的开发集\n",
    "    包含 12,500 个问题对的测试集\n",
    "\n",
    "### 评估方式\n",
    "\n",
    "使用准确率Accuracy来评估，即：\n",
    "\n",
    "$$\n",
    "\\text { 准确率 }(\\text { Accuracy })=\\text { 预测正确的条目数 / 预测总条目数 }\n",
    "$$\n",
    "\n",
    "也可以使用文本相似度与标签的皮尔逊系数进行评估，不匹配的文本相似度应该更低。\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 任务1：数据集读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lib.utils import load_lcqmc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "train, valid, test = load_lcqmc()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Shape of training: (238766, 3)\tvalidation: (8802, 3)\ttest: (12500, 3)\n"
     ]
    }
   ],
   "source": [
    "print(f'Shape of training: {train.shape}\\tvalidation: {valid.shape}\\ttest: {test.shape}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>query1</th>\n",
       "      <th>query2</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>喜欢打篮球的男生喜欢什么样的女生</td>\n",
       "      <td>爱打篮球的男生喜欢什么样的女生</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>我手机丢了，我想换个手机</td>\n",
       "      <td>我想买个新手机，求推荐</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>大家觉得她好看吗</td>\n",
       "      <td>大家觉得跑男好看吗？</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>求秋色之空漫画全集</td>\n",
       "      <td>求秋色之空全集漫画</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>晚上睡觉带着耳机听音乐有什么害处吗？</td>\n",
       "      <td>孕妇可以戴耳机听音乐吗?</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               query1           query2  label\n",
       "0    喜欢打篮球的男生喜欢什么样的女生  爱打篮球的男生喜欢什么样的女生      1\n",
       "1        我手机丢了，我想换个手机      我想买个新手机，求推荐      1\n",
       "2            大家觉得她好看吗       大家觉得跑男好看吗？      0\n",
       "3           求秋色之空漫画全集        求秋色之空全集漫画      1\n",
       "4  晚上睡觉带着耳机听音乐有什么害处吗？     孕妇可以戴耳机听音乐吗?      0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Train.head()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 任务2：文本数据分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 任务3：文本相似度（统计特征）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 任务4：文本相似度（词向量与句子编码）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 任务5：文本匹配模型（LSTM孪生网络）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 任务6：文本匹配模型（Sentence-BERT模型）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 任务7：文本匹配模型（SimCSE模型）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "hw",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "747b0286689d47d2437887b3fa14bd59117dc06b2f96aae2ee6b23ff3c51c6b1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
